﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PersonBlog.BLL;
using PersonBlog.Model;
using PersonBlog.Common;
using System.Data;

namespace PersonBlog.Controllers
{
    public class WeatherController : Controller
    {
        WeatherCityBLL wcBll = new WeatherCityBLL();
        WeatherInfoBLL wiBll = new WeatherInfoBLL();
        AirIndexBLL aiBll = new AirIndexBLL();

        // GET: Weather
        public ActionResult Index(string CityName)
        {
            if (string.IsNullOrWhiteSpace(CityName))
            {
                CityName = "贵阳";
            }
            //得到城市天气信息
            DataSet ds = wiBll.GetNewList();
            WeatherInfoEntity wiEntity = Assistant.DataSetToEntityList<WeatherInfoEntity>(ds, 0).FirstOrDefault(w=>w.CityName== CityName);
            ViewBag.CurrCity = wiEntity;

            //得到所有城市
            DataSet cityDs = wcBll.GetAllList();
            List<WeatherCityEntity> cityList = Assistant.DataSetToEntityList<WeatherCityEntity>(cityDs, 0).ToList();
            ViewBag.CityList = cityList;

            //得到城市过去7天空气质量信息
            DataSet airDs = aiBll.GetLastIndexByCity(CityName);
            List<AirIndexEntity> AirIndexList = Assistant.DataSetToEntityList<AirIndexEntity>(airDs, 0).OrderBy(a=>a.DateTime).ToList();
            ViewBag.AirIndexList = AirIndexList;

            //返回x轴和y轴数据用以生成图表
            string xAxis = "[";
            string yAxis = "[";
            foreach (AirIndexEntity item in AirIndexList)
            {
                xAxis += "'"+Convert.ToDateTime(item.DateTime).ToString("MM月dd日")+"',";
                yAxis += item.AQIIndex + ",";
            }
            ViewBag.xAxis = xAxis.TrimEnd(',') + "]";
            ViewBag.yAxis = yAxis.TrimEnd(',') + "]";

            ViewBag.CityName = wiEntity.CityName;
            return View();
        }
    }
}